package cn.com.note.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import cn.com.note.R;
import cn.com.note.fragment.FragmentSignalTest;
import cn.com.note.fragment.FragmentTest;

/**
 * 测试 一次打开多个 fragment 页面操作重复的问题
 * Created by SunFuLin on 2019/2/22.
 */

public class FragActivity extends AppCompatActivity implements View.OnClickListener {
    FragmentManager fragmentManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frag);
        fragmentManager = getSupportFragmentManager();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt1:
                addFragment();
                break;
            case R.id.bt2:
                addSignalFragment();
                break;
            case R.id.bt_back:
                fragmentPop();
                break;
        }
    }

    private void addFragment() {
        if (!isInStackFragment("FragmentTest")) {
            SwitchFragment(FragmentTest.newInstance("页面" + (a++)), "FragmentTest", this);
        }
    }

    int a = 0;

    private void addSignalFragment() {
        if (!isInStackFragment("FragmentSignalTest")) {
            SwitchFragment(FragmentSignalTest.newInstance("单例模式" + (a++)), "FragmentSignalTest", this);
        }
    }

    public void SwitchFragment1(Fragment newFragment, AppCompatActivity activity) {
        String stack=null;
        if(newFragment instanceof FragmentTest){
            stack=null;
        }else if(newFragment instanceof FragmentSignalTest){
            stack="FragmentSignalTest";
        }
        if(isInStackFragment(stack)){
            return;
        }
        fragmentManager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.addToBackStack(null);
        transaction.replace(R.id.main_fragment1, newFragment);
        transaction.commit();

    }

    public void SwitchFragment(Fragment newFragment, String back, AppCompatActivity activity) {
        String stack=null;
        if(newFragment instanceof FragmentTest){
            stack="FragmentTest";
        }else if(newFragment instanceof FragmentSignalTest){
            stack="FragmentSignalTest";
        }
        if(isInStackFragment(stack)){
            return;
        }
        fragmentManager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.addToBackStack(back);
        transaction.replace(R.id.main_fragment, newFragment);
        transaction.commit();

    }


    public void fragmentPop() {
        a = 0;
        int numS = fragmentManager.getBackStackEntryCount();
        if(numS>1){
            fragmentManager.popBackStack();
        }
        int num = fragmentManager.getBackStackEntryCount();
        String numString = "++++++++++++++++++++++++++++++++++Fragment回退栈数量："+num;
        Log.d("Fragment", numString);
        for (int i = 0; i < num; i++) {
            FragmentManager.BackStackEntry backstatck = fragmentManager.getBackStackEntryAt(i);
            Log.d("Fragment", backstatck.getName());
        }
    }

    /**
     * 检测本地栈中是否存在该类型 Fragment
     *
     * @param stack
     * @return
     */
    public boolean isInStackFragment(String stack) {
        if(stack==null){
            return false;
        }
        fragmentManager = getSupportFragmentManager();
        int num = fragmentManager.getBackStackEntryCount();
        if(num<1){
            return false;
        }
        //拿到栈顶内容
        FragmentManager.BackStackEntry backStack = fragmentManager.getBackStackEntryAt(num-1);
        String name = backStack.getName();
        if (name!=null &&stack.equals(name)) {
            return true;
        }
        return false;
    }
}
